#include <iostream>
#include <stack>
#include <vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

vector<int> inorderTraversal(TreeNode* root) {
    vector<int> ans;
    if (!root) return ans;
    stack<TreeNode*> stk;
    TreeNode* p = root;
    while(p || !stk.empty()){
        while(p){
            stk.push(p);
            p = p->left;
        }
        p = stk.top();
        stk.pop();
        ans.emplace_back(p->val);
        p = p->right;
    }
    return ans;
}